﻿using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wpf.Netdisk.Models.Dto.ChildrenListItem.NetdiskFileInfo;
using Wpf.Netdisk.Models.Entity;
using Wpf.Netdisk.Models.Vo.ChildrenListItem.NetdiskFileInfo;
using Wpf.Netdisk.Services.Common;
using Wpf.Netdisk.Services.Interface;
using Wpf.Netdisk.ViewModels.Base; 
namespace Wpf.Netdisk.ViewModels.ChildrenListItem
{
    public class FileViewModel : NavigationViewModel
    {
        private readonly IDialogHostService _dialogService;
        private bool isOpen = false;

        public bool IsOpen
        {
            get { return isOpen; }
            set { isOpen = value; RaisePropertyChanged(); }
        }

        private ImageSource source;

        public ImageSource Source
        {
            get { return source; }
            set { source = value; RaisePropertyChanged(); }
        }

        private readonly INetdiskFileService _netdiskFileService;
        private readonly IEventAggregator _eventAggregator;
        private string folderId;
        private ObservableCollection<NetdiskFileInfoVo> files;
        public DelegateCommand<string> OpenCommand { get; set; }
        public DelegateCommand<string> DownLoadCommand { get; set; }
        public DelegateCommand ClosePopupCommand { get; set; }
        public ObservableCollection<NetdiskFileInfoVo> Files
        {
            get { return files; }
            set { files = value; RaisePropertyChanged(); }
        }

        public FileViewModel(IContainerProvider containerProvider, 
            INetdiskFileService netdiskFileService, 
            IEventAggregator eventAggregator,
            IDialogHostService dialogHostService) : base(containerProvider)
        {
            this._netdiskFileService = netdiskFileService;
            this._eventAggregator = eventAggregator;
            QueryFileList();
            var getFileList = _eventAggregator.GetEvent<PubSubEvent>();
            getFileList.Subscribe(QueryFileList);
            OpenCommand = new DelegateCommand<string>(Open);
            this._dialogService= dialogHostService;
            ClosePopupCommand = new DelegateCommand(ClosePopup);
            DownLoadCommand = new DelegateCommand<string>(DownLoad);
        }
        public void ClosePopup()
        {
            IsOpen=false;
        }
        public void DownLoad(string fileUrl)
        {
            var dlg = new CommonOpenFileDialog();
            dlg.IsFolderPicker = true; 

            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
            {
                var folderPath = dlg.FileName;
                DownloadFileDto dto = new();
                dto.FileUrl = fileUrl;
                dto.FolderPath = folderPath; 
                WebClient webClient = new(); 
                webClient.Credentials = CredentialCache.DefaultCredentials;
                //webClient.DownloadFile(,);
                //前端给后端传保存路径
                //文件地址给后端 
            } 
        }
        public void Open(string fileUrl)
        {
            
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(fileUrl);
            bitmap.EndInit();
            Source = bitmap;
            IsOpen = true;
        }
        public string FolderId
        {
            get { return folderId; }
            set { folderId = value;RaisePropertyChanged(); }
        }

       

        public override  void OnNavigatedTo(NavigationContext navigationContext)
        {
            //判断是否有这个Key
            if (navigationContext.Parameters.ContainsKey("FolderId"))
            {
                //取出传过来的值
                FolderId = navigationContext.Parameters.GetValue<string>("FolderId");
                QueryFileList();
            } 
        } 
         
        public async void QueryFileList()
        {
            Files = new();
            Files.Clear();
            var fileList=await _netdiskFileService.GetListAsync(new NetdiskFileInfoQueryDto
            {
                FolderId = FolderId,
                Page=1,
                Size=10000
            });
            if (fileList?.Rows !=null&& fileList?.Rows.Count>0)
            {
                foreach (var item in fileList?.Rows)
                {
                    NetdiskFileInfoVo info = new();
                    var name = item["FileName"]?.ToString();
                    var suffix = name.Split('.')[name.Split('.').Count()-1];
                    if (suffix=="jpg"|| suffix == "png")
                    {
                        info.FileType = "Img";
                    } 
                    info.FileName = name;
                    info.FileUrl = item["FileUrl"]?.ToString();
                    info.FolderId = item["FolderId"]?.ToString();
                    info.Id = item["Id"]?.ToString();
                    Files.Add(info);
                }
            }
        }
    }
}
